package com.shu.security;

import java.io.IOException;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.SecurityMetadataSource;
import org.springframework.security.access.intercept.AbstractSecurityInterceptor;
import org.springframework.security.access.intercept.InterceptorStatusToken;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Component;

@Component
public class MyFilterSecurityInterceptor extends AbstractSecurityInterceptor implements Filter {

	@Autowired
	private MyFilterSecurityMetadataSource securityMetadataSource;
	
	@Resource
	private MyAccessDecisionManager accessDecisionManager;

	@Resource
    private AuthenticationConfiguration authenticationConfiguration;
	
	/* get、set方法 */
	public FilterInvocationSecurityMetadataSource getSecurityMetadataSource() {
		return securityMetadataSource;
	}

	public void setSecurityMetadataSource(FilterInvocationSecurityMetadataSource securityMetadataSource) {
		this.securityMetadataSource = (MyFilterSecurityMetadataSource) securityMetadataSource;
	}

	@Override
	public Class<? extends Object> getSecureObjectClass() {
		return FilterInvocation.class;
	}

	@Override
	public SecurityMetadataSource obtainSecurityMetadataSource() {
		return this.securityMetadataSource;
	}
	

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		FilterInvocation fi = new FilterInvocation(request, response, chain);
		invoke(fi);
	}

	public void invoke(FilterInvocation fi) throws IOException, ServletException {
		/**
		 * 最核心的代码就是@link InterceptorStatusToken token =
		 * super.beforeInvocation(fi);
		 * 它会调用我们定义的MyInvocationSecurityMetadataSource.
		 * getAttributes方法和MyAccessDecisionManager.decide方法
		 * 这一句，即在执行doFilter之前，进行权限的检查，而具体的实现已经交给@link MyAccessDecisionManager 了
		 */
		InterceptorStatusToken token = super.beforeInvocation(fi);
		try {
			// 继续走下一个拦截器，也就是org.springframework.security.web.access.intercept.FilterSecurityInterceptor
			fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
		} finally {
			super.afterInvocation(token, null);
		}
	}

	@Override
	public void destroy() {
	}

	@PostConstruct
	private void load() {
		super.setAccessDecisionManager(accessDecisionManager);
        try {
			super.setAuthenticationManager(authenticationConfiguration.getAuthenticationManager());
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	
	public void init(FilterConfig arg0) throws ServletException {
		  
	}


}
